﻿var _iLastPickedIndex = -1;
var _oXml, _oPop, _o, _iPopH, _iPopW, _oXslProc;

var KEY_SELECTALL;
var KEY_ENTER = 13;
var KEY_SPACE = 32;
var KEY_UP = 38;
var KEY_DOWN = 40;
var KEY_LEFT = 37;
var KEY_RIGHT = 39;
var GRID_SUPPORT_MULTISELECT = true;
var _iTotalRecords = 0; var _iNumSelected = 0; 

function window.onload() {
    initGrid();

    // Localizable Select All Key
    KEY_SELECTALL = "A".charCodeAt(0);
}

function initGrid() {

    var i;

    // Handle slow loading parent pages
    if (window.parent.document.readyState == "complete") {
        var iTotalRecords = 0;

        // Get the total number or records if there are any
        //if (typeof(grid) != "undefined" && grid.rows[0])
        if (typeof (grid) != "undefined" && grid.rowcount > 0) {
            //iTotalRecords = grid.rows.length;
            for (i = 0; i < grid.rows.length; i++) {
                if (grid.rows[i].selectable != 0)
                    iTotalRecords++;
            }
        }

        with (window.parent) {
            _iNumSelected = 0;
            _iTotalRecords = iTotalRecords;


        }

        // If there are records in the grid, select the first record
        if (iTotalRecords > 0) {
            //handleSelectRow(grid.rows[0]);
            for (i = 0; i < grid.rows.length; i++) {
                if (grid.rows[i].selectable != 0) {
                    if (!grid.loc || grid.loc.toUpperCase() == grid.rows[i].oid.toUpperCase()) {
                        handleSelectRow(grid.rows[i]);
                        _iLastPickedIndex = i;
                        break;
                    }
                }
            }

            // Bring focus to allow for instant keyboard nav
            // Use setActive to get around child window loosing focus bug
            grid.setActive();
        }

        window.parent._bReady = true;

    }
    else {
        window.setTimeout(initGrid, 10);
    }
}

// Brings up the preview print window containing all details of the object (row item) passed in
function printRowDetails(o) {
    openStdWin("/_forms/print/print.aspx?objectType=" + grid.oname + "&id=" + o.oid + "&subType=" + o.otype, "print" + buildWinName(o.oid));
}

// Automagically sets the title of the NOBR to it's inner text to allow for auto-ellipse text viewing
function RenderToolTip() {
    var o = event.srcElement;

    if (o && o.tagName == "NOBR" && o.title == "" && o.innerText != "") {
        o.title = o.innerText;
    }
}






/*
START USER INPUT CODE
=====================
*/
function handleDblClick() {
    var oTr = event.srcElement;

    while ((oTr.tagName != "TR")) {
        oTr = oTr.parentElement;
        if (oTr == null) return; 	// ignore the dragged cursor command
    }

    if (oTr.selectable != 0)
        window.parent.frameElement.fireEvent("ondblclick");
}

function handleClick(o) {
    if (!o) {
        o = event.srcElement;
    }

    switch (o.tagName) {
        case "TBODY":
            return false;

        case "IMG":
        case "NOBR":
            o = o.parentElement;

        case "TD":
            o = o.parentElement;
            break;
    }

    while (o.tagName != "TR" && o != null) {
        o = o.parentElement;
    }
    if (o == null) return false;

    if (o.selectable == 0) return;

    if (!o.selected) {
        selectRow(o);
        handleSelectRow(o);
    }
    else if (event.ctrlKey && window._iNumSelected > 1) {    
        unselectRow(o);
    }
    else {        
        _iLastPickedIndex = o.rowIndex;
        unselectRows(o);
    }
}

function document.onkeyup() {
    var i;

    if (event.altKey || event.shiftKey || (event.ctrlKey && event.keyCode != KEY_SELECTALL) ||
		typeof (grid) == "undefined") {
        return false;
    }

    switch (event.keyCode) {
        case KEY_SELECTALL:
            if (event.ctrlKey && GRID_SUPPORT_MULTISELECT && window.parent._iTotalRecords > 0) {
                selectAllRows();
            }
            break;

        case KEY_ENTER:

            if (window.parent._iTotalRecords > 0) {
                grid.rows[_iLastPickedIndex].fireEvent("ondblclick");
            }
            break;

        case KEY_DOWN:
            /*
            if (_iLastPickedIndex < grid.rows.length - 1)
            {
            unselectRows();

				var i = _iLastPickedIndex + 1;
            grid.rows[i].fireEvent("onclick");
            }
            */
            for (i = _iLastPickedIndex + 1; i < grid.rows.length; i++) {
                if (grid.rows[i].selectable != 0) {
                    unselectRows();

                    grid.rows[i].fireEvent("onclick");
                    break;
                }
            }
            break;

        case KEY_UP:
            /*
            if (_iLastPickedIndex > 0)
            {
            unselectRows();

				var i = _iLastPickedIndex - 1;
            grid.rows[i].fireEvent("onclick");
            }
            */
            for (i = _iLastPickedIndex - 1; i >= 0; i--) {
                if (grid.rows[i].selectable != 0) {
                    unselectRows();

                    grid.rows[i].fireEvent("onclick");
                    break;
                }
            }
            break;
    }
}

/*
END USER INPUT CODE
===================
*/









/*
START ROW SELECTION HELPER CODE
===============================
*/
function handleSelectRow(o) {
    
    if (event && event.shiftKey && GRID_SUPPORT_MULTISELECT && _iLastPickedIndex != -1) {
        unselectRows(o);

        if (o.rowIndex < _iLastPickedIndex) {
            selectRows(o.rowIndex, _iLastPickedIndex);
        }
        else {
            selectRows(_iLastPickedIndex, o.rowIndex);
        }
    }
    else {
        selectRow(o);

        if (event && !event.ctrlKey || !GRID_SUPPORT_MULTISELECT) {        
            unselectRows(o);
        }

        _iLastPickedIndex = o.rowIndex;
    }
}

function selectRow(o) {
    if (!o.selected && o.selectable != 0) {
        window._iNumSelected++;     
        o.selected = true;
        o.style.paddingTop = "0px";
        applyStyle(o, "gridSelectOn");
    }
}

function unselectRow(o) {
    if (o.selected) {
        window._iNumSelected--;
        o.selected = false;
        o.style.paddingTop = "1px";
        applyStyle(o, "data");
    }
}

function selectAllRows() {
    selectRows(1, grid.rows.length - 1);
}

// Selects a range of rows
function selectRows(iStart, iStop) {
    for (iStart; iStart < iStop + 1; iStart++) {
        selectRow(grid.rows[iStart]);
    }    
}

function selectRowsById(s) {
    if (!s) return;

    if (!window.parent._bReady) {
        setTimeout("selectRowsById(\"" + s + "\")", 10);
        return;
    }

    var arr = s.split(",");
    var i, j;
    for (i = 0; i < grid.rows.length; i++) {
        unselectRow(grid.rows[i]);
    }

    for (i = 0; i < grid.rows.length; i++) {
        //unselectRow(grid.rows[i]);
        if (GRID_SUPPORT_MULTISELECT || window._iNumSelected < 1) {
            for (j = 0; j < arr.length; j++) {
                if (grid.rows[i].oid == arr[j]) {
                    selectRow(grid.rows[i]);
                    break;
                }
            }
        }
        else {
            break;
        }
    }

    window.parent.gridBar.RefreshStatus();
}

// Unselects ALL rows skipping the one provided
function unselectRows(oSkip) {
    var i;
     
    for (i = 0; i < grid.rows.length; i++) {
        var oTr = grid.rows[i];

        if (oTr !== oSkip) {            
            unselectRow(oTr);
        }
    }
    /*  lm 暂时拿掉先
    if (_iNumSelected == 0) {
        for (i = 0; i < grid.rows.length; i++) {
            if (grid.rows[i].selectable != 0) {
                selectRow(grid.rows[i]);
                break;
            }
        }
    }
     */
}

function applyStyle(o, sClass) {
    /*
    var i = 0;
    var ii = o.cells.length;

	while (i < ii)
    {
    o.cells[i].className = sClass;
    i++;
    }
    */
    o.className = sClass;
}

/*
END ROW SELECTION HELPER CODE
=============================
*/










/*
START CONTEXT MENU RELATED SCRIPT
*/

function document.oncontextmenu() {
    _o = event.srcElement;
    event.returnValue = false;

    //右键菜单只有“打开”和“刷新”，意义不大，不如取消
    return false;

    while (_o.tagName != "TR" && _o.tagName != "BODY") {
        _o = _o.parentElement;
    }

    // Disable Popup Items
    popupToggle(_o.tagName != "TR");

    // Create and cache the popup object
    if (!_oPop) {
        _oPop = window.createPopup();

        var oS = _oPop.document.createStyleSheet();
        oS.addRule("HR", "margin-bottom:1px;");
        oS.addRule("div.item", "cursor:hand;padding:3px;padding-left:15px;");

        // Temp Render the Popup for sizing	
        _oPop.show(0, 0, 0, 0);

        with (_oPop.document.body) {
            innerHTML = divMenu.outerHTML;

            with (firstChild) {
                style.display = "";
                onmouseover = popGlow;
                onmouseout = popGlow;
                _iPopH = offsetHeight;
                _iPopW = offsetWidth + 10; // 10px = Padding
            }
        }

        // Cache the current window object for inline script to call
        _oPop.document.oParent = window;
    }

    _oPop.show(event.screenX - 1, event.screenY - 5, _iPopW, _iPopH);
}

function popGlow() {
    o = _oPop.document.parentWindow.event.srcElement;

    if (o.tagName == "NOBR") {
        o = o.parentElement;
    }

    if (o.id == "divMenu" || o.tagName == "HR") {
        return;
    }

    with (o.runtimeStyle) {
        if (color == "highlighttext") {
            color = backgroundColor = "";
        }
        else {
            color = "highlighttext";
            backgroundColor = "highlight";
        }
    }
}

function popupToggle(b) {
    // The NOBR Tag causes a bug in .children so we have to use .ALL
    var o = divMenu.all.tags("div");

    // "Magic" code to determine what should be disabled and not
    if (o.length > 1 && o[0].disabled != b) {
        o[0].disabled = o[1].disabled = b;
        _oPop = null;
    }
}

/*
END CONTEXT MENU RELATED SCRIPT
*/
